<?php

namespace app\controllers\home;

use Yii;
use yii\web\Controller;


class IndexController extends Controller
{
	public $layout = FALSE;
	
    public function actionAdd()
    {
    	$data[] = time();
    	$data[] = time()-3600*24;
    	$data[] = time()-3600*24*2;

    	$session = Yii::$app->session;
        $session->open();
        if ($session->has('id')) {
        	$id = $session->get('id');

            //查询用户的日报用于判断前两天是否添加了日报，如果未添加可以补填
	    	$query = Yii::$app->db->createCommand('SELECT daily_date FROM daily WHERE uid=:uid')
	                ->bindValue(':uid',$id)
	                ->queryAll();
	    	return $this->render('add',['data'=>$data,'query'=>$query]);
        } else {
        	return $this->redirect('/index.php/home/user/login',301);
        }
        
    }

    public function actionInsert()
    {
    	$request = YII::$app->request->post();

        $id = Yii::$app->session->get('id');;
    	$params = [
            'uid' => $id, 
            'record' => $request['record'], 
            'conclusion' => $request['conclusion'],
            'plan' => $request['plan'],
            'daily_date' => $request['daily_date'],
            'ctime' => time()
        ];

        if (empty($params['record']) || empty($params['conclusion']) || empty($params['plan'])) {
            return $this->redirect('add',301);
        }

        $query = Yii::$app->db->createCommand('SELECT daily_date FROM daily WHERE uid=:uid ')
                    ->bindValue(':uid',$id)
                    ->queryAll();
    

        //判断当天是否已添加过日报，如果已添加则返回日报列表页    
        foreach ($query as $key => $value) {
            $y = date('Ymd',$params['daily_date']);
            $t = date('Ymd',$value['daily_date']);
            if ($y == $t) {
                return $this->redirect('/index.php/home/user/daily',301);
            }
        }

		$res = Yii::$app->db->createCommand()->insert('daily',$params)->execute(); 
		if ($res) {
			return $this->redirect('/index.php/home/user/daily',301);
		} else {
			return $this->redirect('add');
		}       
		
    }

    //显示编辑日报的页面
    public function actionUpdate()
    {
        $request = YII::$app->request->get();
        $session = Yii::$app->session;
        $session->open();
        if ($session->has('id')) {
            $id = $session->get('id');

            $query = Yii::$app->db->createCommand('SELECT * FROM daily WHERE uid=:uid')
                    ->bindValue(':uid',$id)
                    ->queryAll();
            
            foreach ($query as $key => $value) {
                $y = date('Ymd',$value['ctime']);
                $t = date('Ymd',time());
                if ($y == $t) {
                    return  $this->render('update',['query'=>$query[$key]]);
                }
            }
        }
    }

    public function actionNew()
    {
        $request = YII::$app->request->post();

        $sql = "UPDATE daily SET record='".$request['record']."' ,conclusion='".$request['conclusion']."' ,plan='".$request['plan']."' ,daily_date='".$request['daily_date']."' ,ctime='".time()."' WHERE id=".$request['rid'];

        //判断提交的数据是否为空
        if (empty($request['record']) || empty($request['conclusion']) || empty($request['plan'])) {
            return $this->redirect('/index.php/home/user/daily',301);
        }

        $res = Yii::$app->db->createCommand($sql)->execute(); 
    
        if ($res) {
            return $this->redirect('/index.php/home/user/daily',301);
        } else {
            return $this->redirect('add');
        } 
    }
}
